/************************************************************************/
/*  									*/
/*  fullsearch_MSE.c							*/
/*									*/
/*  Tom Lookabaugh							*/
/*  August 13, 1987							*/
/*  Last Revision: August 14, 1987					*/
/*									*/
/*  DESCRIPTION								*/
/*	Full search through a codebook.					*/
/*	MSE Version.							*/
/*  RETURNS								*/
/*	Returns distortion between trvect and the closest entry in	*/
/*	the codebook.  Puts index of closest entry into storage pointed	*/
/*	to by bestcdwd.							*/
/*  CALLS								*/
/*	none.								*/
/*									*/
/************************************************************************/

#include "stdvq.h"
#include <math.h>

double fullsearch(cdbk, cdbksz, trvect, bestcdwd)

    double *cdbk;	/* codebook					*/
    int cdbksz;		/* number of vectors in codebook		*/
    double *trvect;	/* training sequence vector			*/
    int *bestcdwd;	/* points to index of closest codeword		*/

{

    int c, d;		/* iteration counters				*/
    double presdist;	/* present distortion				*/
    double bestdist;	/* best distortion				*/
    double double_temp; /* temporary storage				*/
	
    bestdist = HUGE;

    for (c = 0; c < cdbksz; c++) {
	presdist = 0.0;
	for (d = 0; d < dim; d++) {
	    double_temp = cdbk[c*cdbkdim + d] - trvect[d];
	    presdist += double_temp*double_temp;
	    if (presdist > bestdist) break;
	}
	if (presdist <  bestdist) {
	    bestdist = presdist;
	    *bestcdwd = c;
	}
    }
    return(bestdist);
}
	
